<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Account extends CI_Controller {

    public function index() {
        echo 'Raté';
    }

    public function login() {
        $this->load->view('account/login');
    }

    public function admin() {
        $users = $this->db
                ->select(array(
                    'UTILISATEUR_ID',
                    'UTILISATEUR_NOM',
                    'UTILISATEUR_PRENOM',
                    'UTILISATEUR_EMAIL'
                ))
                ->from('utilisateur')
                ->get()
                ->result()
        ;

        $data['users'] = $users;

        $this->layout
                ->title('Administration des utilisateurs')
                ->js('account/admin')
                ->view('account/admin', $data)
        ;
    }

    public function process() {
        $email = $this->security->xss_clean($this->input->post('email'));
        $pass = $this->security->xss_clean($this->input->post('mdp'));

        $this->load->library('encrypt');
        $pass = $this->encrypt->sha1($pass);

        $this->load->model('account_model');
        $result = $this->account_model->validate($email, $pass);

        if ($result) {
            $res['result'] = 'success';
        } else {
            $res['result'] = 'error';
            $res['msg'] = '<font color=red>Email et/ou mot de passe invalide.</font><br />';
        }

        echo json_encode($res);
    }

    public function do_logout() {
        $this->session->sess_destroy();
        redirect(site_url() . 'account/login');
    }

    public function adminAdd() {
        $this->adminEdit(0);
    }

    public function adminEdit($idUser) {
        $data = array();
        $user = $this->db->select(array(
                    'UTILISATEUR_ID',
                    'UTILISATEUR_NOM',
                    'UTILISATEUR_PRENOM',
                    'UTILISATEUR_EMAIL',
                    'UTILISATEUR_PASSWORD'
                ))
                ->from('utilisateur')
                ->where('UTILISATEUR_ID', $idUser)
                ->get()
                ->result()
        ;

        if (count($user) == 1)
            $data['user'] = $user[0];

        $this->load->view('account/adminEdit', $data);
    }

    public function adminDelete($idUser) {
        $this->load->view('account/adminDelete');
    }

    public function edit() {
        $idUser = $this->input->post('idUser');
        $name = $this->input->post('name');
        $surname = $this->input->post('surname');
        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $repassword = $this->input->post('repassword');


        if ($password == $repassword) {
            $changePass = false;
            if ($password != '')
                $changePass = true;
            $pass = sha1($password);
            $data = array(
                'UTILISATEUR_NOM' => $name,
                'UTILISATEUR_PRENOM' => $surname,
                'UTILISATEUR_EMAIL' => $email
            );
            if ($changePass)
                $data['UTILISATEUR_PASSWORD'] = $pass;

            if ($idUser != '')
                $this->db->where('UTILISATEUR_ID', $idUser)->update('utilisateur', $data);
            else {
                if ($changePass)
                    $this->db->insert('utilisateur', $data);
                else {
                    $res['result'] = 'error';
                    $res['field'] = 'editPassword';
                    $res['msg'] = '<br/>Le password est obligatoire.';
                }
            }

            $res['result'] = 'success';
        } else {
            $res['result'] = 'error';
            $res['field'] = 'editPassword';
            $res['msg'] = '<br/>Les password ne correspondent pas.';
        }


        echo json_encode($res);
    }

    public function delete() {
        $idUser = $this->input->post('idUser');
        $this->db->where('UTILISATEUR_ID', $idUser)->delete('utilisateur');
    }

}